package com.sylingd.blog.activity

import android.app.Activity
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.Toolbar
import android.widget.FrameLayout
import com.sylingd.blog.BaseApplication
import com.sylingd.blog.R
import com.sylingd.blog.ui.index.ListFragment

class ListActivity : AppCompatActivity() {

	private var type: Int = 0
	private lateinit var mFragment: ListFragment

	companion object {
		fun launch(context: Activity, tagName: String) {
			val intent = Intent(context, ListActivity::class.java)
			intent.putExtra("type", ListFragment.TYPE_TAG)
			intent.putExtra("tag", tagName)
			context.startActivity(intent)
		}
		fun launch(context: Activity, id: Int) {
			val intent = Intent(context, ListActivity::class.java)
			intent.putExtra("type", ListFragment.TYPE_ID)
			intent.putExtra("id", id)
			context.startActivity(intent)
		}
	}

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_list)

		val toolbar = findViewById<Toolbar>(R.id.toolbar)
		setSupportActionBar(toolbar)

		supportActionBar?.setDisplayHomeAsUpEnabled(true)
		toolbar.setNavigationOnClickListener {
			finish()
		}

		BaseApplication.initSlidr(this)

		//获取类型
		type = intent.getIntExtra("type", ListFragment.TYPE_ID)
		when (type) {
			ListFragment.TYPE_ID -> {
				mFragment = ListFragment.create(intent.getIntExtra("id", 0))
			}
			ListFragment.TYPE_TAG -> {
				mFragment = ListFragment.create(intent.getStringExtra("tag"))
			}
		}

		val t = this.fragmentManager.beginTransaction()
		t.replace(R.id.frame, mFragment)
		t.commit()
	}
}
